﻿/**********************************************
 * 功能名称：原材料查询
编写人：  杨莹
建立时间： 杨 2008年11月18日
说明：
根据材料的ID，材料的名称查询。可分页显示。每页显示20行记录。
 * 
 * 
 **************************************************************/


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;

namespace Hisun.View.Query
{
    public partial class MeterialQuery : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {

                Panel_MainQuery.Visible = false; 
            }
        }


        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex*20;
            switch (DropDownList_Field.SelectedIndex)
            {
                case  0 :
                var MyQuery = (from a in  db.TABC_JS_Meterial where a.材料ID.Contains(TextBox_QueryValue.Text) orderby a.材料ID select a).Skip(skipAccount).Take(20);
                GridView_MeterialQuery.DataSource = MyQuery;
                GridView_MeterialQuery.DataBind();
                if (MyQuery.Count() == 0)
                { Panel_MainQuery.Visible = false; }
                else
                { Panel_MainQuery.Visible = true; };
                break;
                case  1 :
                var MyQuery2 = (from a in db.TABC_JS_Meterial where a.材料名称.Contains(TextBox_QueryValue.Text)  orderby a.材料ID select  a).Skip(skipAccount).Take(20);
                GridView_MeterialQuery.DataSource = MyQuery2;
                GridView_MeterialQuery.DataBind();
                if (MyQuery2.Count() == 0)
                { Panel_MainQuery.Visible = false; }
                else
                { Panel_MainQuery.Visible = true; };
                break;                      
            }


            
        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            switch (DropDownList_Field.SelectedIndex)
            {
                case 0:
                    MyQuery = (from a in db.TABC_JS_Meterial where a.材料ID.Contains(TextBox_QueryValue.Text) select a).Count();                   
                    break;
                case 1:
                    MyQuery = (from a in db.TABC_JS_Meterial where a.材料名称.Contains(TextBox_QueryValue.Text) select a).Count();
         
                    break;
            }
            
         return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {
           

            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();

            

            int pages = (Totals % 20) == 0 ? (Totals / 20) : (Totals / 20 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":
                    
                            pageIndx = 0;
                      
                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;                     
                }
            
            } 
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx-1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx =pageIndx+1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }
            
            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx+1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }












    }
}
